﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using BEPUphysics.Collidables;
using BEPUphysics;
using BEPUphysics.MathExtensions;

namespace EventFlowLibrary.LevelComponents
{
    internal class StaticModel : BaseModel
    {


        /// <summary>
        /// Whether the object casts a shadow in the game world.
        /// </summary>
        internal bool DisplayShadow { get { return displayShadow; } }
        private bool displayShadow;

        /// <summary>
        /// Whether the object receives a shadow in the game world.
        /// </summary>
        internal bool ReceiveShadow { get { return receiveShadow; } }
        private bool receiveShadow;

        /// <summary>
        /// Track our static mesh to remove/readd when moving models
        /// </summary>
        private StaticMesh staticMesh;
        private Space gameSpace;

        /// <summary>
        /// Construct the static model.
        /// </summary>
        /// <param name="_staticModel">The model to display.</param>
        /// <param name="_worldMatrix">The world matrix for the model.</param>
        internal StaticModel(string _id, Model _model, Vector3 _scale, Vector3 _rotation, Vector3 _position, Space _gameSpace, StaticMesh _staticMesh, bool _displayShadow, bool _receiveShadow)
            : base(_id, _model, _scale, _rotation, _position)
        {
            gameSpace = _gameSpace;
            staticMesh = _staticMesh;
            displayShadow = _displayShadow;
            receiveShadow = _receiveShadow;
        }

        internal override void SetPosition(Vector3 _position)
        {
            base.SetPosition(_position);

            if (this.staticMesh != null)
            {
                this.staticMesh.WorldTransform = new AffineTransform(this.Scale,
                                                        Quaternion.CreateFromRotationMatrix(Matrix.CreateRotationX(this.Rotation.X) * Matrix.CreateRotationY(this.Rotation.Y) * Matrix.CreateRotationZ(this.Rotation.Z)),
                                                        this.Position);

                this.TransformedBoundingBox = staticMesh.BoundingBox;
            }
        }

        internal override void SetRotation(Vector3 _rotation)
        {
            base.SetRotation(_rotation);

            if (this.staticMesh != null)
            {
                this.staticMesh.WorldTransform = new AffineTransform(this.Scale,
                                            Quaternion.CreateFromRotationMatrix(Matrix.CreateRotationX(this.Rotation.X) * Matrix.CreateRotationY(this.Rotation.Y) * Matrix.CreateRotationZ(this.Rotation.Z)),
                                            this.Position);

                this.TransformedBoundingBox = staticMesh.BoundingBox;
            }
        }

        internal override void SetScale(Vector3 _scale)
        {
            base.SetScale(_scale);

            if (this.staticMesh != null)
            {
                this.staticMesh.WorldTransform = new AffineTransform(this.Scale,
                                            Quaternion.CreateFromRotationMatrix(Matrix.CreateRotationX(this.Rotation.X) * Matrix.CreateRotationY(this.Rotation.Y) * Matrix.CreateRotationZ(this.Rotation.Z)),
                                            this.Position);

                this.TransformedBoundingBox = staticMesh.BoundingBox;
            }
        }
    }
}
